可用指令
資料結構
從左邊插入value.
# 從左邊插入value
127.0.0.1:6379> lpush mylist abc
(integer) 1
# 確認底層型態為quicklist
127.0.0.1:6379> object encoding mylist
"quicklist"
127.0.0.1:6379> lpush mylist abc abc abc
(integer) 3
# value可以重覆
127.0.0.1:6379> lrange mylist 0 3
1) "abc"
2) "abc"
3) "abc"
當list存在時,從左邊插入value.
127.0.0.1:6379> lrange mylist 0 3
(empty array)
127.0.0.1:6379> lpush mylist abc
(integer) 1
# 從左邊插入值到已存在的list
127.0.0.1:6379> lpushx mylist def
(integer) 2
127.0.0.1:6379> lrange mylist 0 3
1) "def"
2) "abc"
# 無法插入值到不存在的list
127.0.0.1:6379> lpushx mylist2 def
(integer) 0
127.0.0.1:6379> lrange mylist2 0 3
(empty array)
從左邊取出value,可以指定一次取出多少個value.
# 取出value
127.0.0.1:6379> lpop mylist
"abc"
# 沒有可以再取出的value
127.0.0.1:6379> lpop mylist
(nil)
# 插入3個value
127.0.0.1:6379> lpush mylist abc def ghi
(integer) 3
127.0.0.1:6379> lrange mylist 0 2
1) "ghi"
2) "def"
3) "abc"
# 一次取出兩個value
127.0.0.1:6379> lpop mylist 2
1) "ghi"
2) "def"
從右邊插入value.
# 從右邊插入value
127.0.0.1:6379> rpush mylist abc
(integer) 1
# 確認底層型態為quicklist
127.0.0.1:6379> object encoding mylist
"quicklist"
當list存在時,從右邊插入value.
127.0.0.1:6379> lrange mylist 0 3
(empty array)
127.0.0.1:6379> rpush mylist abc
(integer) 1
# 從右邊插入值到已存在的list
127.0.0.1:6379> rpushx mylist def
(integer) 2
127.0.0.1:6379> lrange mylist 0 3
1) "abc"
2) "def"
# 無法插入值到不存在的list
127.0.0.1:6379> rpushx mylist2 def
(integer) 0
從右邊取出value,可以指定一次取出多少個value.
# 取出value
127.0.0.1:6379> rpop mylist
"abc"
# 沒有可以再取出的value
127.0.0.1:6379> rpop mylist
(nil)
# 插入3個value
127.0.0.1:6379> rpush mylist abc def ghi
(integer) 3
127.0.0.1:6379> lrange mylist 0 2
1) "abc"
2) "def"
3) "ghi"
# 一次取出兩個value
127.0.0.1:6379> rpop mylist 2
1) "ghi"
2) "def"
顯示List中valu的個數.
# 插入3個value
127.0.0.1:6379> lpush mylist abc def ghi
(integer) 3
# 取得List中value的個數
127.0.0.1:6379> llen mylist
(integer) 3
# <----
# -3 -2 -1 Right index
# abc def ghi
#Left index 0 1 2
# ---->
取得指定Range的value值.
127.0.0.1:6379> lpush mylist abc def ghi
(integer) 3
# 取出從位置0到位置-1的value
127.0.0.1:6379> lrange mylist 0 -1
1) "ghi"
2) "def"
3) "abc"
# 取出從位置1到位置1的value
127.0.0.1:6379> lrange mylist 1 1
1) "def"
# 取出從位置1到位置2的value
127.0.0.1:6379> lrange mylist 1 2
1) "def"
2) "abc"
# 取出從位置-2到位置1的value
127.0.0.1:6379> lrange mylist -2 1
1) "def"
# 取出從位置-2到位置2的value
127.0.0.1:6379> lrange mylist -2 2
1) "def"
2) "abc"
# 取出從位置-2到位置0的value
127.0.0.1:6379> lrange mylist -2 0
(empty array)
取得指定index的value值.
127.0.0.1:6379> lpush mylist abc def ghi
(integer) 3
# 查詢index 1的value值
127.0.0.1:6379> lindex mylist 1
"def"
# 查詢index 2的value值
127.0.0.1:6379> lindex mylist 2
"abc"
修改指定index的value值.
127.0.0.1:6379> lindex mylist 0
"ghi"
# 修改
127.0.0.1:6379> lset mylist 0 aaa
OK
127.0.0.1:6379> lindex mylist 0
"aaa"
根據index範圍保留value,除範圍以內的其餘value將被刪除.
127.0.0.1:6379> lrange mylist 0 2
1) "aaa"
2) "def"
3) "abc"
# 刪除指定範圍以外的value
127.0.0.1:6379> ltrim mylist 1 2
OK
127.0.0.1:6379> lrange mylist 0 2
1) "def"
2) "abc"
block 右邊取出,類似long polling機制.
127.0.0.1:6379> lrange mylist 0 3
(empty array)
# 如果list中沒有value,則會等待10秒
127.0.0.1:6379> brpop mylist 10
(nil)
(10.03s)
# 如果10秒內有value插入,則從右邊取出1個value
127.0.0.1:6379> brpop mylist 10
1) "mylist"
2) "aaa"
(1.64s)
block 左邊取出,類似long polling機制.
127.0.0.1:6379> lrange mylist 0 3
(empty array)
# 如果list中沒有value,則會等待10秒
127.0.0.1:6379> blpop mylist 10
(nil)
(10.03s)
# 如果10秒內有value插入,則從左邊取出1個value
127.0.0.1:6379> blpop mylist 10
1) "mylist"
2) "aaa"
(1.32s)
從左邊開始刪除幾個指定value.
127.0.0.1:6379> lpush mylist abc abc abc
(integer) 3
127.0.0.1:6379> lrange mylist 0 3
1) "abc"
2) "abc"
3) "abc"
# 從左邊開始刪除1個abc的value
127.0.0.1:6379> lrem mylist 1 abc
(integer) 1
127.0.0.1:6379> lrange mylist 0 3
1) "abc"
2) "abc"
# 從左邊開始刪除2個abc的value
127.0.0.1:6379> lrem mylist 2 abc
(integer) 2
127.0.0.1:6379> lrange mylist 0 3
(empty array)
將value插入到指定value之前或之後.
127.0.0.1:6379> lpush mylist abc def
(integer) 2
127.0.0.1:6379> lrange mylist 0 3
1) "def"
2) "abc"
# 在abc此value前插入111的value
127.0.0.1:6379> linsert mylist before abc 111
(integer) 3
127.0.0.1:6379> lrange mylist 0 3
1) "def"
2) "111"
3) "abc"
# 在111此value後插入222的value
127.0.0.1:6379> linsert mylist after 111 222
(integer) 4
127.0.0.1:6379> lrange mylist 0 3
1) "def"
2) "111"
3) "222"
4) "abc"
指定從某個list取出value並插入到另外一個list.
127.0.0.1:6379> lrange mylist 0 3
(empty array)
127.0.0.1:6379> lpush mylist abc def
(integer) 2
127.0.0.1:6379> lrange mylist2 0 3
(empty array)
#從mylist中取出value abc插入mylist2
127.0.0.1:6379> rpoplpush mylist mylist2
"abc"
127.0.0.1:6379> lrange mylist 0 3
1) "def"
127.0.0.1:6379> lrange mylist2 0 3
1) "abc"
指定從某個list的某方向取出value並插入到另外一個list的指定方向.
127.0.0.1:6379> lrange mylist 0 3
(empty array)
127.0.0.1:6379> lrange mylist2 0 3
(empty array)
127.0.0.1:6379> lpush mylist abc def ghi
(integer) 3
# 從mylist的右邊取出value並插入到mylist2的右邊
127.0.0.1:6379> lmove mylist mylist2 right right
"abc"
127.0.0.1:6379> lrange mylist 0 3
1) "ghi"
2) "def"
127.0.0.1:6379> lrange mylist2 0 3
1) "abc"
# 從mylist的左邊取出value並插入到mylist2的右邊
127.0.0.1:6379> lmove mylist mylist2 left right
"ghi"
127.0.0.1:6379> lrange mylist 0 3
1) "def"
127.0.0.1:6379> lrange mylist2 0 3
1) "abc"
2) "ghi"
在6.2版本改用BLMOVE取代.
跟LMOVE使用方式一樣,但會指定秒數.
指定從某個list的某方向取出value並插入到另外一個list的指定方向.